<?php
class Comment_model extends Base_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    /**
     * @param $data
     * @return array
     * 获取 评论
     */
    public function getActivityOrTrackComments($data)
    {

        if(!empty($data['type']))
        {
            $this->db->where('type', $data['type']);
        }
        if(!empty($data['id']))
        {
            $key = empty($data['key'])?'track_id':$data['key'];
            $this->db->where("{$key}", $data['id']);
        }

        if (!empty($data['$offset']) && !empty($data['limit'])) {
            $this->db->limit($data['$offset'], $data['limit']);
        }

        $this->db->select('co.comments,co.tourist_pic,co.created,co.track_score,u.userName,u.userIcon');
        $this->db->from('mod_track_comments as co');
        $this->db->join('user_v2 as u', 'u.uid = co.uid', 'left');
        $query = $this->db->get()->result_array();
        $items = array();
        foreach ($query as $q)
        {
            $item = array();
            $item['comments']=$q['comments'];
            $item['track_score']=$this->img('/resource/www/imgs/score_'.$q['track_score'].'.png');
            $item['userName'] = $q['userName'];
            $item['userIcon'] = $this->img($q['userIcon']);
            $item['created'] = empty($q['created'])?'':date('Y.m.d H:i',$q['created']);
            //评论图片
            $q['tourist_pic']=trim($q['tourist_pic']);
            $q['tourist_pic']=rtrim($q['tourist_pic'],';');
            $ret = array();
            
            if(!empty($q['tourist_pic'])){
            	if(stripos($q['tourist_pic'],';')){
            		$imgs = explode(';',$q['tourist_pic']);
            		foreach($imgs as $img)
            		{
            			if(!empty($img))
            				$ret[] = $this->img($img);
            		}
            	}else{
            		$ret=array($this->img($q['tourist_pic']));
            	}
            }
            
            
            $item['tourist_pic'] = $ret;
            $items[] = $item;
        }

        return $items;
    }
    
    public function getJingdianComments($data)
    {
    	if(!empty($data['id']))
    	{
    		$key = empty($data['key'])?'targetId':$data['key'];
    		$this->db->where("{$key}", $data['id']);
    	}
    	
    	if (!empty($data['$offset']) && !empty($data['limit'])) {
    		$this->db->limit($data['$offset'], $data['limit']);
    	}
    	
    	$this->db->select('co.comments,co.tourist_pic,co.created,u.userName,u.userIcon');
    	$this->db->from('mod_scenic_comments as co');
    	$this->db->join('user_v2 as u', 'u.uid = co.uid', 'left');
    	$query = $this->db->get()->result_array();
    	$items = array();
    	foreach ($query as $q)
    	{
    		$item = array();
    		$item['comments']=$q['comments'];
    		$item['userName'] = $q['userName'];
    		$item['userIcon'] = $this->img($q['userIcon']);
    		$item['created'] = empty($q['created'])?'':date('Y.m.d H:i',$q['created']);
    		//评论图片
    		$q['tourist_pic']=trim($q['tourist_pic']);
    		$q['tourist_pic']=rtrim($q['tourist_pic'],';');
    		$ret = array();
    	
    		if(!empty($q['tourist_pic'])){
    			if(stripos($q['tourist_pic'],';')){
    				$imgs = explode(';',$q['tourist_pic']);
    				foreach($imgs as $img)
    				{
    					if(!empty($img))
    						$ret[] = $this->img($img);
    				}
    			}else{
    				$ret=array($this->img($q['tourist_pic']));
    			}
    		}
    	
    	
    		$item['tourist_pic'] = $ret;
    		$items[] = $item;
    	}
    	
    	return $items;
    }

}
